#include "device/Timer.h"

Timer::Timer() {
    timer[0] = 0;
    timer[1] = 0;
    boot_time = get_time_us();
}

bool Timer::read(word_t start_addr, size_t length, uint8_t* buffer) {
    word_t offset = start_addr - TIMER_BASE;
    assert(offset == 0 || offset == 4);
    assert(length == 4);
    if (offset == 4) {
        auto us = get_time();
        timer[0] = (uint32_t)us;
        timer[1] = us >> 32;
    }
    uint8_t* time_base = (uint8_t*)timer;
    std::memcpy(buffer, time_base + offset, length);
    return true;
}

bool Timer::write(word_t start_addr, size_t length, const uint8_t* buffer) {
    return false;
}

void Timer::update() {
    return;
}

uint64_t Timer::get_time() {
    uint64_t time = get_time_us() - boot_time;
    return time;
}
